<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="getUserList">
          查询
        </el-button>
        <el-button @click="clearRoleName">清空</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="showAddRoleDialog">添加</el-button>
    <el-button type="danger" :disabled="true">批量删除</el-button>

    <el-table border :data="roleList" class="mt-20" v-loading="loading">
      <el-table-column align="center" width="60" type="selection" />
      <el-table-column align="center" label="序号" width="100" type="index" />
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column align="center" label="操作">
        <template v-slot="{ row }">
          <el-button
            :icon="InfoFilled"
            type="info"
            size="small"
            @click="distributeAuth(row)"
          />
          <el-button
            :icon="Edit"
            type="warning"
            size="small"
            @click="showUpdateRoleDialog(row)"
          />
          <el-popconfirm
            @confirm="deleteUser(row.id)"
            :title="`你确定要删除角色 ${row.roleName}吗？`"
          >
            <template #reference>
              <el-button :icon="Delete" type="danger" size="small" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getUserList"
      @current-change="getUserList"
    />

    <el-dialog
      v-model="roleDialogVisible"
      :title="`${roleFormData.id ? '修改' : '添加'}角色`"
    >
      <el-form ref="roleFormRef" :model="roleFormData" :rules="roleFormRules">
        <el-form-item label="请输入角色名称" prop="roleName">
          <el-input placeholder="角色名称" v-model="roleFormData.roleName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="roleDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XRole",
};
</script>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import {
  getRoleListApi,
  deleteRoleApi,
  addRoleApi,
  updateRoleApi,
} from "@/api/acl/role";
import type { GetUserList, GetUserItem } from "@/api/acl/model/userModel";
import { Delete, Edit, Search, InfoFilled } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
/********************************分配权限**************************************/
const router = useRouter();
const distributeAuth = (row: GetUserItem) => {
  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
/********************************修改角色**************************************/
const prevRoleName = ref("");
const showUpdateRoleDialog = (row: GetUserItem) => {
  // 赋值
  roleFormData.roleName = row.roleName;
  prevRoleName.value = row.roleName;
  roleFormData.id = row.id;
  // 清除校验规则
  roleFormRef.value?.clearValidate();
  // 显示对话框
  roleDialogVisible.value = true;
};
/********************************添加角色**************************************/
const roleDialogVisible = ref(false);
// 显示对话框
const showAddRoleDialog = () => {
  // 清空表单
  roleFormData.roleName = "";
  roleFormData.id = "";
  // 清除校验规则
  roleFormRef.value?.clearValidate();
  // 显示对话框
  roleDialogVisible.value = true;
};
// 点击确定按钮
const confirm = async () => {
  // 校验表单
  await roleFormRef.value.validate();
  const id = roleFormData.id;
  // 发送请求
  if (id) {
    // 修改角色
    if (roleFormData.roleName === prevRoleName.value) {
      // 关闭对话框
      roleDialogVisible.value = false;
      // 提示用户
      ElMessage.warning("修改前后角色名相同，此次修改无效");
      return;
    }
    await updateRoleApi(roleFormData);
  } else {
    // 添加角色
    await addRoleApi(roleFormData.roleName);
  }
  // 获得最新数据
  getUserList();
  // 关闭对话框
  roleDialogVisible.value = false;
  // 提示用户
  ElMessage.success(`${id ? "修改" : "添加"}用户成功`);
};
// 校验表单
const roleFormRef = ref();
const roleFormData = reactive({
  roleName: "",
  id: "",
});
const roleFormRules = reactive({
  roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
/********************************删除角色**************************************/
const deleteUser = async (id: string) => {
  await deleteRoleApi(id);
  getUserList();
  ElMessage.success("删除角色成功");
};
/********************************数据展示**************************************/
const roleName = ref("");
const roleList = ref<GetUserList>([]);
const loading = ref(false);

const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
// 封装的公共发送请求的函数
const getUserList = async () => {
  loading.value = true;
  const res = await getRoleListApi({
    page: currentPage.value,
    limit: pageSize.value,
    roleName: roleName.value,
  });
  total.value = res.total;
  roleList.value = res.items;
  loading.value = false;
};
// 挂载的时候发送请求
onMounted(getUserList);
// 清空按钮
const clearRoleName = () => {
  roleName.value = "";
  getUserList();
};
</script>

<style scoped></style>
